package com.cscj.android.repository.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.cscj.android.repository.db.entity.BrowseHistoryEntity
import kotlinx.coroutines.flow.Flow

@Dao
interface BrowserHistoryDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    suspend fun addBrowserHistory(browseHistoryEntity: BrowseHistoryEntity)

    @Query("select * from browse_history where id != ${BrowseHistoryEntity.RootId} order by visitTime desc")
    fun browserHistoryList(): Flow<List<BrowseHistoryEntity>>

    @Query("select * from browse_history order by id")
    fun allHistoryList(): Flow<List<BrowseHistoryEntity>>

    @Query("select * from browse_history where id != ${BrowseHistoryEntity.RootId} and title like :keyword or url like :keyword order by visitTime desc")
    fun browserHistoryList(keyword: String): Flow<List<BrowseHistoryEntity>>


    @Query("delete from browse_history where id in (:ids)")
    suspend fun deleteByIds(ids: IntArray)

    @Query("delete from browse_history where id != ${BrowseHistoryEntity.RootId}")
    suspend fun deleteAll()

    @Query("update browse_history set icon = :favicon where url = :url")
    suspend fun updateFavicon(url:String, favicon: String?)

}